﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using bus.console.service.bus.vo;
using System.Web.Script.Serialization;
using web.ui.Service_MemberReg;

public partial class Service_MemberReg : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            doAction();
        }
    }

    private void doAction()
    {
        String sou_type = Request.QueryString["sou_type"];
        String sou_id = Request.QueryString["sou_id"];
        String dev_token = Request.QueryString["dev_token"];

        if (!String.IsNullOrEmpty(sou_type) && !String.IsNullOrEmpty(sou_id) && !String.IsNullOrEmpty(dev_token))
        {
            Member m = new Member();

            m.ID = Guid.NewGuid().ToString();
            m.SourceType = sou_type;
            m.SourceID = sou_id;
            m.DeviceToken = dev_token;
            m.InsDT = DateTime.Now;
            m.UpdDT = DateTime.Now;

            MemberProfile profile = new MemberProfile();
            profile.MemberID = m.ID;
            profile.JoyPoint = 0;
            profile.MaxNoticeNum = 0;
            profile.PushNotice = 1;
            profile.InsDT = DateTime.Now;
            profile.UpdDT = DateTime.Now;
            busService.myService.Dao.DaoInsert(m);
            busService.myService.Dao.DaoInsert(profile);

            JavaScriptSerializer serializer = new JavaScriptSerializer();


            ResultShowVO vo = new ResultShowVO();
            vo.member = toShowMember(m);
            vo.profile = toShowProfile(profile);

            String json = serializer.Serialize(vo);
            Response.Write(json);
            Response.End();
        }
        else 
        {
            ResultShowVO vo = new ResultShowVO();
            vo.member = new MemberShowVO();
            vo.profile = new MemberProfileShowVO();

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            String json = serializer.Serialize(vo);
            Response.Write(json);
            Response.End();
        }
    }

    private MemberProfileShowVO toShowProfile(MemberProfile profile)
    {
        MemberProfileShowVO s = new MemberProfileShowVO();
    
        s.point = profile.JoyPoint;
        s.max_notice = profile.MaxNoticeNum;
        s.push = profile.PushNotice;
        return s;
    }

    private MemberShowVO toShowMember(Member m)
    {
        MemberShowVO s = new MemberShowVO();
        s.id = m.ID;
        s.dev_token = m.DeviceToken;
        s.sou_id = m.SourceID;
        s.sou_type = m.SourceType;
        return s;
    }
}
namespace web.ui.Service_MemberReg 
{
    class ResultShowVO 
    {
       public virtual  MemberShowVO member { get; set; }
       public virtual  MemberProfileShowVO profile { get; set; }

    }

    class MemberShowVO 
    {
        public String id  {get;set;}

        /// <summary>
        ///  facebook
        /// </summary>
        public virtual string sou_type { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public virtual string sou_id { get; set; }


        public virtual string dev_token { get; set; }
    }

    class MemberProfileShowVO
    {
        public virtual int push { get; set; }

        /// <summary>
        /// 每天最多接收幾則推播0:無限制
        /// </summary>
        public virtual int max_notice { get; set; }

        /// <summary>
        /// 樂點幣
        /// </summary>
        public virtual int point { get; set; }
    }
}